<?php
/**
 * CreateTime: 2024/1/22 11:35
 * User:LinQ
 */

namespace App\Http\Service\Goods;

use App\Func\User\UserFunc;
use App\Http\Service\BasicInterfaceService;
use App\Models\Client\ClientModel;
use App\Models\Goods\GoodsTypeModel;

class ManageGoodsTypeService implements BasicInterfaceService
{

    public function getList(array $prams)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $goodsTypeModel = new GoodsTypeModel();
        $this->listSelectDefaultFilters($goodsTypeModel);
        if($filters){
            $this->listSelectFilters($goodsTypeModel,$filters);
        }
        $res = $goodsTypeModel->cListHasCom($goodsTypeModel,$page_size,$desc,[
            '*',
            'com_id as com_str'
        ]);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.goods_type')
        ]));
    }

    public function listSelectDefaultFilters($query){
        $query->pushFilter('status',1);
    }

    public function dispose($data): mixed
    {
        return $data;
    }

    public function listSelectFilters($query, $filters)
    {
        if(map_has_value($filters,'goods_type_name')){
            $query->pushFilter('goods_type_name',$filters['goods_type_name'],'like');
        }
        if(map_has_value($filters,'pid')){
            $query->pushFilter('pid',$filters['pid']);
        }
    }

    public function getSelect()
    {
        $clientModel = new GoodsTypeModel();
        $res = $clientModel->scopeAddComIdWhere()->select([
            'goods_type_id as key',
            'goods_type_name as value'
        ])->where('status',1)->get();
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.goods_type')
        ]));
    }

    public function getSelectHasPage($prams)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $clientModel = new GoodsTypeModel();
        $this->listSelectDefaultFilters($clientModel);
        if($filters){
            $this->listSelectFilters($clientModel,$filters);
        }
        $res = $clientModel->cListHasCom($clientModel,$page_size,$desc,[
            'goods_type_id as key',
            'goods_type_name as value'
        ]);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.goods_type')
        ]));
    }

    public function getInfo($id)
    {

    }

    public function create($data)
    {
        $comId = (new UserFunc())->getCurrentComId();
        $goodsTypeModel = new GoodsTypeModel();
        // 同一个公司类型不能有重名
        $isExistsInfoGoodsTypeName = $goodsTypeModel->scopeAddComIdWhere()->where('status',1)->where('goods_type_name',$data['goods_type_name'])->first();
        if($isExistsInfoGoodsTypeName){
            s_fail_return(null,trans('message.is_exists',[
                'name' => trans('key.goods_type')
            ]));
        }
        $insertData = [
            'pid' => 0,
            'com_id' => $comId,
            'status' => 1,
            'goods_type_name' => $data['goods_type_name'],
            'created_at' => date('Y-m-d h:i:s')
        ];
        if(isset($data['pid'])&&$data['pid']){
            // 检查当前公司下是否存在这个商品类型
            $isExistsInfo = $goodsTypeModel->where('status',1)
                ->scopeAddComIdWhere()
                ->where('goods_type_id',$data['pid'])
                ->first();
            if(!$isExistsInfo){
                s_fail_return(null,trans('message.not_exists',[
                    'name' => trans('key.parent_goods_type')
                ]));
            }
            $insertData['pid'] = $data['pid'];
        }

        $res = $goodsTypeModel->insertGetId($insertData);
        s_c_return($res,trans('message.created_ok',[
            'name'=>trans('key.goods_type')
        ]));
    }

    public function fakeDelete($id)
    {

    }

    public function fakeDeleteByIds($ids)
    {

    }

    public function delete($id)
    {

    }

    public function update($data)
    {
        $goodsTypeModel = new GoodsTypeModel();
        // 检查是否存在
        $isExistsInfoGoodsType = $goodsTypeModel
            ->scopeAddComIdWhere()
            ->where('status',1)
            ->where('goods_type_id',$data['goods_type_id'])
            ->first();
        if(!$isExistsInfoGoodsType){
            s_fail_return(null,trans('message.not_exists',[
                'name' => trans('key.goods_type')
            ]));
        }

        // 如果存在pid
        if(map_has_value($data,'pid')){
            // 检查当前类型是否是别的类型的上级
            if($data['pid']==$data['goods_type_id']){
                s_fail_return(null,trans('message.p_not_self'));
            }
            $exists = $goodsTypeModel
                ->scopeAddComIdWhere()
                ->where('status',1)
                ->where('goods_type_id',$data['goods_type_id'])
                ->first();
            if($exists){
                s_fail_return(null,trans('message.has_child',[
                    'name' => trans('key.goods_type')
                    ]).trans('message.update_err',[
                    'name' => ''
                    ]));
            }
        }
        $updateData = [];
        if(map_has_value($data,'goods_type_name')){
            $updateData['goods_type_name'] = $data['goods_type_name'];
        }
        if(map_has_value($data,'pid')){
            $updateData['pid'] = $data['pid'];
        }
        $goodsTypeModel->where('goods_type_id',$data['goods_type_id'])->update($updateData);
        s_c_return(null,trans('message.update_ok',[
            'name' => trans('key.goods_type')
        ]));
    }
}
